//Copyright 2010 Balazs Keri, Imre Keri
//
//This file is part of FireworksCreator.
//
//FireworksCreator is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//FireworksCreator is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with FireworksCreator.  If not, see <http://www.gnu.org/licenses/>.
//
#include <QComboBox>
#include <QDoubleValidator>
#include <QLabel>
#include <QLineEdit>
#include <QVBoxLayout>
#include "randomdoublewidget.h"
//
const double double_min=-1000000.0;
const double double_max=1000000.0;
const int double_decimals=5;
//
RandomDoubleWidget::RandomDoubleWidget(QWidget *p_parent)
:
   QWidget(p_parent),
   value(model::RandomDouble(new model::RandomDouble::Constant())),
   null_value(true),
   top_layout(new QVBoxLayout()),
   type_combo(new QComboBox(this)),
   constant_edit(0),
   interval_top_layout(0),
   interval_min_label(0),
   interval_min_edit(0),
   interval_max_label(0),
   interval_max_edit(0),
   error_label(0)
{
   construct();
}
RandomDoubleWidget::RandomDoubleWidget(model::RandomDouble p_value,QWidget *p_parent)
:
   QWidget(p_parent),
   value(p_value),
   null_value(true),
   top_layout(new QVBoxLayout()),
   type_combo(new QComboBox()),
   constant_edit(0),
   interval_top_layout(0),
   interval_min_label(0),
   interval_min_edit(0),
   interval_max_label(0),
   interval_max_edit(0),
   error_label(0)
{
   construct();
}
//
model::RandomDouble RandomDoubleWidget::get_value() const
{
   return value;
}
void RandomDoubleWidget::set_value(model::RandomDouble p_value)
{
   if(null_value||p_value.data().get_type()!=value.data().get_type())
   {
      delete_widget();
      null_value=false;
      value=p_value;
      setup_widget();
      update_type_combo();
   }
   else
   {
      value=p_value;
      update_widget();
   }
}
bool RandomDoubleWidget::is_null_value() const
{
   return null_value;
}
void RandomDoubleWidget::set_null_value(bool p_null_value)
{
   if(p_null_value!=null_value)
   {
      delete_widget();
      null_value=p_null_value;
      setup_widget();
      update_type_combo();
   }
}
//
void RandomDoubleWidget::on_type_currentIndexChanged(int p_i)
{
   QVariant data(type_combo->itemData(p_i));
   if(!data.isValid())
      return;
   delete_widget();
   switch(data.toInt())
   {
   case model::RandomDouble::type_constant:
      value=model::RandomDouble(new model::RandomDouble::Constant());
      break;
   case model::RandomDouble::type_interval:
      value=model::RandomDouble(new model::RandomDouble::Interval());
      break;
   default:
      break;
   }
   setup_widget();
   emit value_changed();
}
void RandomDoubleWidget::on_constant_editingFinished()
{
   if(!constant_edit->isModified())
      return;
   value.as_constant().set_value(QLocale::system().toDouble(constant_edit->text()));
   emit value_changed();
}
void RandomDoubleWidget::on_interval_min_editingFinished()
{
   if(!interval_min_edit->isModified())
      return;
   value.as_interval().set_min(QLocale::system().toDouble(interval_min_edit->text()));
   emit value_changed();
}
void RandomDoubleWidget::on_interval_max_editingFinished()
{
   if(!interval_max_edit->isModified())
      return;
   value.as_interval().set_max(QLocale::system().toDouble(interval_max_edit->text()));
   emit value_changed();
}
//
void RandomDoubleWidget::construct()
{
   setLayout(top_layout);
   top_layout->setMargin(0);
   top_layout->setSpacing(0);
   type_combo->setEditable(false);
   type_combo->addItem(tr("constant","randomdouble_type"),model::RandomDouble::type_constant);
   type_combo->addItem(tr("interval","randomdouble_type"),model::RandomDouble::type_interval);
   top_layout->addWidget(type_combo,0,Qt::AlignTop);
   top_layout->addStretch(1);
   setup_widget();
   update_type_combo();
   connect(type_combo,SIGNAL(currentIndexChanged(int)),SLOT(on_type_currentIndexChanged(int)));
}
void RandomDoubleWidget::update_type_combo()
{
   type_combo->blockSignals(true);
   if(null_value)
      type_combo->setCurrentIndex(-1);
   else
      type_combo->setCurrentIndex(type_combo->findData(value.data().get_type()));
   type_combo->setEnabled(!null_value);
   type_combo->blockSignals(false);
}
void RandomDoubleWidget::setup_widget()
{
   if(null_value)
      setup_null_widget();
   else
   {
      switch(value.data().get_type())
      {
      case model::RandomDouble::type_constant:
         setup_constant_widget();
         update_constant_widget();
         break;
      case model::RandomDouble::type_interval:
         setup_interval_widget();
         update_interval_widget();
         break;
      default:
         setup_error_widget();
         break;
      }
   }
}
void RandomDoubleWidget::delete_widget()
{
   if(null_value)
      delete_null_widget();
   else
   {
      switch(value.data().get_type())
      {
      case model::RandomDouble::type_constant:
         delete_constant_widget();
         break;
      case model::RandomDouble::type_interval:
         delete_interval_widget();
         break;
      default:
         delete_error_widget();
         break;
      }
   }
}
void RandomDoubleWidget::update_widget()
{
   if(!null_value)
   {
      switch(value.data().get_type())
      {
      case model::RandomDouble::type_constant:
         update_constant_widget();
         break;
      case model::RandomDouble::type_interval:
         update_interval_widget();
         break;
      default:
         break;
      }
   }
}
void RandomDoubleWidget::setup_null_widget()
{
}
void RandomDoubleWidget::delete_null_widget()
{
}
void RandomDoubleWidget::setup_constant_widget()
{
   constant_edit=new QLineEdit(this);
   constant_edit->setValidator(new QDoubleValidator(double_min,double_max,double_decimals));
   top_layout->insertWidget(1,constant_edit,0,Qt::AlignTop);
   connect(constant_edit,SIGNAL(editingFinished()),SLOT(on_constant_editingFinished()));
}
void RandomDoubleWidget::delete_constant_widget()
{
   top_layout->removeWidget(constant_edit);
   constant_edit->deleteLater();
   constant_edit=0;
}
void RandomDoubleWidget::update_constant_widget()
{
   constant_edit->blockSignals(true);
   constant_edit->setText(QLocale::system().toString(value.as_constant().get_value()));
   constant_edit->blockSignals(false);
}
void RandomDoubleWidget::setup_interval_widget()
{
   interval_min_edit=new QLineEdit();
   interval_min_edit->setValidator(new QDoubleValidator(double_min,double_max,double_decimals));
   interval_max_edit=new QLineEdit();
   interval_max_edit->setValidator(new QDoubleValidator(double_min,double_max,double_decimals));
   interval_min_label=new QLabel(tr("min"));
   interval_min_label->setFrameStyle(QFrame::Panel|QFrame::Sunken);
   interval_max_label=new QLabel(tr("max"));
   interval_max_label->setFrameStyle(QFrame::Panel|QFrame::Sunken);
   interval_top_layout=new QGridLayout();
   interval_top_layout->addWidget(interval_min_label,0,0);
   interval_top_layout->addWidget(interval_min_edit,0,1);
   interval_top_layout->addWidget(interval_max_label,1,0);
   interval_top_layout->addWidget(interval_max_edit,1,1);
   top_layout->insertLayout(1,interval_top_layout,0);
   connect(interval_min_edit,SIGNAL(editingFinished()),SLOT(on_interval_min_editingFinished()));
   connect(interval_max_edit,SIGNAL(editingFinished()),SLOT(on_interval_max_editingFinished()));
}
void RandomDoubleWidget::delete_interval_widget()
{
   top_layout->removeItem(top_layout->itemAt(1));
   interval_min_edit->deleteLater();
   interval_max_edit->deleteLater();
   interval_min_label->deleteLater();
   interval_max_label->deleteLater();
   interval_top_layout->deleteLater();
   interval_min_edit=0;
   interval_max_edit=0;
   interval_min_label=0;
   interval_max_label=0;
   interval_top_layout=0;
}
void RandomDoubleWidget::update_interval_widget()
{
   interval_min_edit->blockSignals(true);
   interval_min_edit->setText(QLocale::system().toString(value.as_interval().get_min()));
   interval_min_edit->blockSignals(false);
   interval_max_edit->blockSignals(true);
   interval_max_edit->setText(QLocale::system().toString(value.as_interval().get_max()));
   interval_max_edit->blockSignals(false);
}
void RandomDoubleWidget::setup_error_widget()
{
   error_label=new QLabel(tr("<<unknown type>>"));
   error_label->setFrameStyle(QFrame::Box||QFrame::Raised);
   top_layout->insertWidget(1,error_label,1);
}
void RandomDoubleWidget::delete_error_widget()
{
   top_layout->removeWidget(error_label);
   error_label->deleteLater();
   error_label=0;
}
